package com.oauth.facebook.client.ui.tabs;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.facebook.client.beans.FacebookUser;
import com.oauth.facebook.client.rpc.FacebookRemoteServiceAsync;
import com.oauth.facebook.client.ui.ConnectionPanel;

public class InboxTab extends Composite {

	private static InboxTabUiBinder uiBinder = GWT.create(InboxTabUiBinder.class);

	interface InboxTabUiBinder extends UiBinder<Widget, InboxTab> {
	}

	@UiField
	VerticalPanel inboxPanel;

	@UiField
	Image loadingImage;

	@UiField
	HorizontalPanel loadingPanel;

	private FacebookRemoteServiceAsync service;

	public InboxTab(FacebookRemoteServiceAsync pService) {
		service = pService;
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void init() {
		loadingImage.setVisible(true);

		inboxPanel.getElement().setInnerHTML("");

		service.getConnections(new OAuthAsnyCallBack<List<FacebookUser>>() {

			@Override
			public void onSuccess(List<FacebookUser> pResult) {
				if (pResult.isEmpty()) {
					inboxPanel.add(new Label("You don't have any message in your inbox"));
				} else {
					for (FacebookUser user : pResult) {
						inboxPanel.add(new ConnectionPanel(user));
					}
				}
				loadingImage.setVisible(false);
			}

		});
	}
}
